package ai.people.platform.jx.service.impl;

import ai.people.netmon.framework.domain.common.NameValueEntity;
import ai.people.netmon.framework.domain.common.SimpleNameValueRespVO;
import ai.people.netmon.framework.domain.jx.AccountPosition;
import ai.people.netmon.framework.domain.jx.CitySortDTO;
import ai.people.platform.jx.helper.TgLocationHelper;
import ai.people.platform.jx.mapper.AccountPositionMapper;
import ai.people.platform.jx.service.IAccountPositionService;
import cn.hutool.core.lang.Pair;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LiGuanNan
 * @date 2022/6/29 20:53
 */
@Service
public class AccountPositionServiceImpl implements IAccountPositionService {

    /**
     * 帐户位置本地缓存
     */
    private final Map<String, List<AccountPosition>> cityAccountPositionMap = Maps.newConcurrentMap();

    @Autowired
    private AccountPositionMapper accountPositionMapper;

    private static Map<String, String> cityNameMap = Maps.newHashMap();

    private static Map<String, String> cityNewNameMap = Maps.newHashMap();

    static {
        cityNameMap.put("beijing", "China-BeiJing");
        cityNameMap.put("guiyang", "China-GuiYang");
        cityNameMap.put("nanning", "China-NanNing");
        cityNameMap.put("lisboa", "Portugal-Lisboa");
        cityNameMap.put("taipei", "China-Taipei");
        cityNameMap.put("hongkong", "China-HongKong");
        cityNameMap.put("zhangjiakou", "China-ZhangJiaKou");
        cityNameMap.put("hangzhou", "China-HangZhou");
        cityNameMap.put("shenzhen", "China-ShenZhen");
        cityNameMap.put("sanfrancisco", "USA-San Francisco");
        cityNameMap.put("chongqing", "China-ChongQing");
        cityNameMap.put("chicago", "USA-Chicago");
        cityNameMap.put("washingtonDist", "USA-Washington D.C");
        cityNameMap.put("losangeles", "USA-Los Angeles");
        cityNameMap.put("jifu", "Ukraine-Kyiv");
        cityNameMap.put("newyork", "USA-Newyork");
        cityNameMap.put("wuhan", "China-WuHan");
        cityNameMap.put("khanabad", "Khanabad-Khanabad");
        cityNameMap.put("qingdao", "China-QingDao");
        cityNameMap.put("qinhuangdao", "China-QinHuangDao");
        cityNameMap.put("hengshui", "China-HengShui");
        cityNameMap.put("aomen", "China-Macao");
        cityNameMap.put("wuxi", "China-WuXi");
        cityNameMap.put("tianjin", "China-TianJin");
        cityNameMap.put("zhuhai", "China-zhuHai");
        cityNameMap.put("langfang", "China-LangFang");
        cityNameMap.put("jinan", "China-JiNan");
        cityNameMap.put("wulumuqi", "China-WuLuMuQi");
        cityNameMap.put("hebi", "China-Hebi");
        cityNameMap.put("Dharamshala", "India-Dharamshala");
        cityNameMap.put("dongying", "China-DongYing");
        cityNameMap.put("pingdingshan", "China-PingDingShan");
        cityNameMap.put("taizhou", "China-TaiZhou");
        cityNameMap.put("gannan", "China-GanNan");
        cityNameMap.put("texas", "USA-Texas");
        cityNameMap.put("zhengzhou", "China-ZhengZhou");
        cityNameMap.put("puyang", "China-PuYang");
        cityNameMap.put("dezhou", "China-DeZhou");
        cityNameMap.put("kaifeng", "China-KaiFeng");
        cityNameMap.put("ningxia", "China-NingXia");
        cityNameMap.put("binzhou", "China-BinZhou");
        cityNameMap.put("xuchang", "China-XuChang");
        cityNameMap.put("qiqihaer", "China-QiQiHaEr");
        cityNameMap.put("lasa", "China-Lasa");
        cityNameMap.put("lanzhou", "China-LanZhou");
        cityNameMap.put("yantai", "China-YanTai");
        cityNameMap.put("zibo", "China-ZiBo");
        cityNameMap.put("shandong", "China-ShanDong");

        cityNewNameMap = cityNameMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
    }

    @Override
    public List<SimpleNameValueRespVO> cities() {
        return accountPositionMapper.cityOverView().stream()
                .map(this::getCitySortDTO)
                .sorted(Comparator.comparing(CitySortDTO::getSort).reversed().thenComparing(CitySortDTO::getName))
                .map(o-> new SimpleNameValueRespVO(o.getName(), o.getValue()))
                .collect(Collectors.toList());
    }

    private CitySortDTO getCitySortDTO(NameValueEntity<String, Long> o) {
        CitySortDTO dto = new CitySortDTO();
        dto.setName(o.getName());
        dto.setValue(o.getValue());
        if (o.getName().contains("Egypt")) {
            dto.setSort(99);
        }else if(o.getName().contains("South Africa")){
            dto.setSort(98);
        }else if(o.getName().contains("-")){
            dto.setSort(50);
        }else {
            dto.setSort(0);
        }
        dto.setName(cityNameMap.getOrDefault(o.getName(), o.getName()));
        return dto;
    }

    @Override
    public List<AccountPosition> accountPoints(String city) {
        city = cityNewNameMap.getOrDefault(city, city);

        if(!CollectionUtils.isEmpty(cityAccountPositionMap.get(city))){
            return cityAccountPositionMap.get(city);
        }
        List<AccountPosition> accountPositions = accountPositionMapper.selectList(new LambdaQueryWrapper<AccountPosition>().eq(AccountPosition::getCity, city));
        //位置打散
        accountPositions.forEach(o->{
            Pair<Double, Double> location = TgLocationHelper.getLocation(o.getLongtitude(), o.getLatitide(), o.getDistance());
            o.setLatitide(location.getKey());
            o.setLongtitude(location.getValue());
        });
        cityAccountPositionMap.put(city,accountPositions);
        return accountPositions;
    }

    @Override
    public void clearCache(String city) {
        cityAccountPositionMap.remove(city);
    }
}
